/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  RediaSearchableTableViewCell.m
//  Table with search field
//
//  Created by Mads Darø Kristensen on 9/25/13.
//  Copyright (c) 2013 Redia a/s. All rights reserved.
//

#import "BASearchableTableViewCell.h"

@interface BASearchableTableViewCell ()

@property (strong, nonatomic) UIFont *font;
@property (strong, nonatomic) UIFont *highlightFont;

@end

@implementation BASearchableTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setTitleFont:(UIFont *)font
{
    _font = font;
    self.titleLabel.font = font;
}

- (void)setHighlightFont:(UIFont *)font
{
    _highlightFont = font;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    if (selected) {
        self.titleLabel.font = self.highlightFont;
        self.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:self.titleLabel.text attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
    }
    else {
        self.titleLabel.font = self.font;
        [self setHighlightDepth:_highlightDepth];
    }
}

- (void)setHighlightDepth:(NSInteger)highlightDepth
{
    _highlightDepth = highlightDepth;
    NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:self.titleLabel.text];
    [mutableString setAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} range:NSMakeRange(0, highlightDepth)];
    [mutableString setAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} range:NSMakeRange(highlightDepth, self.titleLabel.text.length - highlightDepth)];
    self.titleLabel.attributedText = mutableString;
}

@end
